<?php
include("../../tools/session.php");

// 如果登录成功
if ($is_login_success === true)
{
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="utf-8">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <meta http-equiv="Content-Language" content="UTF-8" />
  <meta content="all" name="robots" />
  <meta name="author" content="tiger"/>
  <meta name="Copyright" content="jinzhou group" />
  <meta name="description" content="jinzhou group oa" />
  <title>生成公司－用户数组</title>
  <script language="javascript">
    function produce()
    {
	   window.location = "produce_company_user.php?opt=1";
	}
  </script>
</head>
<body>
<?php
// 加载路径数据
require_once("filepaths.php");

$opt = $_GET["opt"];
if (!$opt)
{
?>
  <input type="button" onclick="produce();" value="生成" />
<?php
  exit();
}

	include("../../tools/mysql.php");
	db_connect();
   
	$company = array();
	
	$sql = "SELECT DISTINCT company_id, zh_name, power FROM company ORDER BY company_id ASC";
	$re1 = db_query($sql);
	foreach ($re1 as $v)
	{
		$new_array = array(array(), $v['zh_name'], $v['power']);
		$company[$v['company_id']] = $new_array; 
	}
	
	// print_r($company);
	
	$sql = "SELECT a.company_id, a.user_id, b.username, b.power_add, b.power_trim
			FROM company_user as a, user as b
			WHERE a.user_id = b.id
			ORDER BY a.company_id, a.user_id ASC";
	$re2 = db_query($sql);
	foreach ($re2 as $v)
	{
		$new_array = array($v['user_id'], $v['username'], $v['power_add'], $v['power_trim']);
		$company[$v['company_id']][0][] = $new_array;
	}
	
	$result_str = json_encode($company);
	$last_str = "var __company_user__ = " . $result_str . ";";
	// echo $result_str;
	// print_r($company);
	
	
	/* -------------- 将生成的数据写入文件 -------------- */
	$filename = "data_company_user.js";
	$handle = fopen($file_paths["produced"] . $filename, "w");
	
	if (!fwrite($handle, $last_str))
	{
		echo "生成数据文件 " . $file_paths["produced"] . $filename . " 失败！";
	}
	else 
	{
		echo "生成数据文件 " . $file_paths["produced"] . $filename . " 成功！";
		copy($file_paths["produced"] . $filename, $file_paths["data_js"] . $filename);
	}
   
	fclose($handle);
?>
<?php
}
// 如果登录失败或还未登录
else
{
   session_hop_page("../index.php");
   exit();
}
	  
?>
</body>
</html>
